/*
 * Pressure_Sensor.h
 *
 * Created: 1/27/2013 7:51:56 PM
 *  Author: Parag
 */ 


#ifndef PRESSURE_SENSOR_H_
#define PRESSURE_SENSOR_H_


#define PressureDDR		DDRB
#define PressurePort	PORTB
#define Pressure_PIN	PINB
#define Pressure_SS		PB4										// Pressure Sensor Chip Enabled
#define allowedAmountOfStaleData	5

#include <avr/io.h>
#include <util/delay.h>
#include <avr/io.h>
#include <stdbool.h>
#include <stdint.h>

typedef enum {
	Good_Data		=	0x00,
	Command_Mode	=	0x01,
	Stale_Data		=	0x02,
	Malfunction		=	0x03
} PS_Status;	



/************************************************************************/
/* Name: PressureInitialize
	This initializes and set the Data Direction of Pressure CS ONLY
/************************************************************************/
void PressureInitialize(void);


/************************************************************************/
/*	Name: getPressure
	Calibrated output values for pressure
	are updated at approximately 2 kHz.

	This returns the Raw compensated output WITH THE STATUS Bits
	follow with get Pressure status to get usable pressure data  

	IMPORTANT: always make sure to run InitPressureSPI from SPI.h before using this
	This Sensor works at SPI mode 0; Gyro works at SPI mode 3                                                                 */
/************************************************************************/
void Pressure_Get(uint16_t * output);



/*
	Name:getPressureStatus
	Description: get the Pressure sensor status
	/---------------------------------------
	0-------------------------Normal Operations
	1-------------------------Device in Command Mode(We dont have that feature)
	2-------------------------Stale Data: Data that has already been fetched since the last
							  measurement cycle or data fetched before the first measurement been completed or not updated data
							  This is different from the fact that the Pressure sensor gives Calibrated output values at 2KHz
	3-------------------------Diagnostic Condition/ Loss of sense element or Short Circuit of sense element

	This function also changes the output data into usable form Masking out the status BITS
*/						  
uint8_t Pressure_Get_Status(uint16_t * output);

/************************************************************************/
/*	Name: IsEverythingOk
	Checks to see how frequently we get Bad data
	If we get stale data "allowedAmountOfStaleData" amount of times then return False
	if we get Malfunction status then return False
	else its all good and return True                                                                    */
/************************************************************************/

bool Pressure_Data_Okay(PS_Status stat);


bool Pressure_Read_Sensor(uint16_t *output);
#endif /* PRESSURE_SENSOR_H_ */